package com.web.admin.web.base.core;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.core.facade.BankFacade;
import com.web.base.core.message.BankRequest;
import com.web.base.core.message.BankResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 银行控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月19日
 */
@Controller("coreBankController")
@Http(url = "/base/core")
public class BankController
{
	@Autowired
	private BankFacade bankFacade;
	
	private static DictParser parser = DictParser.getInstance();
	
	public BankController()
	{
	}

	@Http(url = "/bank_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		BankRequest bankRequest = new BankRequest();
		Page<BankResponse> page = bankFacade.listPage(bankRequest, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/base/core/bank_list")
						.put("model", bankRequest)
						.put("map", parser.getStringMap())
						.put("page", page)
						.build();
	}

	@Http(url = "/bank_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		BankRequest bankRequest = request.getPostObject(BankRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		Page<BankResponse> page = bankFacade.listPage(bankRequest, start, Const.PAGE_SIZE);
		return View.createBuilder().setView("/base/core/bank_list_grid")
						.put("page", page)
						.build();
    }

    @Http(url = "/bank_get", method = HttpMethod.GET)
    public View get(HttpRequest request)
    {
        int id = request.getQueryInt("id", 0);
        BankResponse response = null;
        if (id > 0)
        {
            response = bankFacade.get(id);
        }
        return View.createBuilder().setView("/base/core/bank_get")
                        .put("model", response == null ? new BankResponse() : response)
                        .put("map", parser.getStringMap())
                        .build();
    }

    @Http(url = "/bank_save", method = HttpMethod.POST)
    public void save(HttpRequest request)
    {
        BankRequest bankRequest = request.getPostObject(BankRequest.class);
        BankResponse response = null;
        if (bankRequest.getId() == null)
        {
            ValidationUtil.valid(bankRequest, SaveModel.class);
            response = bankFacade.add(bankRequest);
        }
        else
        {
            ValidationUtil.valid(bankRequest, UpdateModel.class);
            response = bankFacade.update(bankRequest);
        }
    }

    @Http(url = "/bank_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
    {

    }

}
